<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Introduction</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="Java 2D, Java, tutorial, introduction, programming, Graphics, multiplatform">
<meta name="description" content="This part of the Java 2D tutorial covers Java 2D basics.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>


<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content">


<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Introduction</h1>


<p>
In this part of the Java 2D tutorial, we will introduce the Java 2D technology. 
</p>


<h2>About</h2>

<p>
This is Java 2D tutorial. It is aimed at beginners. This tutorial 
will teach you basics of programming in Java 2D.
The images used in this tutorial can be downloaded <a href="/img/gfx/java2d/images.tgz">here</a>.
</p>

<div class="center">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* top_horizontal */
google_ad_slot = "3327173442";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<h2>Vector graphics</h2>

<p>
There are two different computer graphics. Vector and raster graphics. 
Raster graphics represent images as a collection of pixels. Vector graphics 
is the use of geometrical primitives such as points, lines, curves or 
polygons to represent images. These primitives are created using mathematical 
equations. Both types of computer graphics have advantages and disadvantages. 
The advantages of vector graphics are:
</p>

<ul>
<li>smaller size</li>
<li>ability to zoom indefinitely</li>
<li>moving, scaling, filling or rotating does not degrade the quality of an image</li>
</ul>


<p>
The <b>Java 2D API</b> provides tools to work with both 
vector and raster (bitmap) graphics. 
</p>


<h2>Java 2D API</h2>

<p>
<b>Java 2D</b> is an API for drawing two-dimensional graphics 
using the Java programming language. 
</p>

<p>
The Java 2D API provides following capabilities:
</p>

<ul>
<li>A uniform rendering model for display devices and printers</li>
<li>A wide range of geometric primitives</li>
<li>Hit detection on shapes, text, and images</li>
<li>A compositing model</li>
<li>Enhanced color support</li>
<li>Printing documents</li> 
<li>Control of the quality of the rendering</li>
</ul>

<p>
The Java 2D API enhances the graphics, text, and imaging capabilities of the 
Abstract Windowing Toolkit (AWT). <b>AWT</b> was the original toolkit for creating 
user interfaces and graphics in Java. For compatibility purposes, Java 2D
is technically a superset of the AWT toolkit. 
</p>

<p>
Java 2D is a powerful technology. It can be used to create rich user interfaces, 
games, animations, multimedia applications or various special effects. 
</p>


<h2>Java 2D skeleton</h2>

<p>
Next we will show a skeleton of a Java 2D application. 
</p>

<div class="codehead">Skeleton.java</div>
<pre class="code">
package com.zetcode;

import java.awt.Graphics;

import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Skeleton extends JPanel {

    public void paint(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;

        g2d.drawString("Java 2D", 50, 50);
    }


    public static void main(String[] args) {

        JFrame frame = new JFrame("Java 2D Skeleton");
        frame.add(new Skeleton());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(280, 240);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
</pre>

<p>
This code repeats throughout the Java 2D tutorial. We will go through it once here. 
</p>


<pre class="explanation">
public void paint(Graphics g) {
   ...
}
</pre>

<p>
All the drawing is done in the <code>paint()</code> method. 
</p>


<pre class="explanation">
Graphics2D g2d = (Graphics2D) g;
</pre>

<p>
The <code>Graphics2D</code> class is a fundamental class for 
rendering graphics in Java 2D. It represents number of devices in a generic way. 
It extends the old Graphics object. This casting is necessary to 
get access to all advanced operations. 
</p>

<pre class="explanation">
g2d.drawString("Java 2D", 50, 50);
</pre> 

<p>
Here we draw a string on the panel. 
</p>

<img src="/img/gfx/java2d/skeleton.png" alt="Java 2D Skeleton">
<div class="figure">Figure: Java 2D skeleton</div>


<p>
This part of the Java 2D tutorial was an introduction to the Java 2D
library.
</p>


<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 
<br>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>

<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified October 25, 2008  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

